<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<link rel="STYLESHEET" href="lib.css" type='text/css' />
<link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" />
<link rel='start' href='../index.html' title='Python documentation Index' />
<link rel="first" href="lib.html" title='Python library Reference' />
<link rel='contents' href='contents.html' title="Contents" />
<link rel='index' href='genindex.html' title='Index' />
<link rel='last' href='about.html' title='About this document...' />
<link rel='help' href='about.html' title='About this document...' />
<link rel="next" href="node149.html" />
<link rel="prev" href="module-email.parser.html" />
<link rel="parent" href="module-email.parser.html" />
<link rel="next" href="node149.html" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name='aesop' content='information' />
<title>7.1.2.1 FeedParser API</title>
</head>
<body>
<div class="navigation">
<div id='top-navigation-panel' xml:id='top-navigation-panel'>
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="7.1.2 parsing email messages"
  href="module-email.parser.html"><img src='../icons/previous.png'
  border='0' height='32'  alt='Previous Page' width='32' /></a></td>
<td class='online-navigation'><a rel="parent" title="7.1.2 parsing email messages"
  href="module-email.parser.html"><img src='../icons/up.png'
  border='0' height='32'  alt='Up one Level' width='32' /></a></td>
<td class='online-navigation'><a rel="next" title="7.1.2.2 parser class API"
  href="node149.html"><img src='../icons/next.png'
  border='0' height='32'  alt='Next Page' width='32' /></a></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
  href="contents.html"><img src='../icons/contents.png'
  border='0' height='32'  alt='Contents' width='32' /></a></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
  border='0' height='32'  alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
  href="genindex.html"><img src='../icons/index.png'
  border='0' height='32'  alt='Index' width='32' /></a></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="module-email.parser.html">7.1.2 Parsing email messages</a>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="module-email.parser.html">7.1.2 Parsing email messages</a>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="node149.html">7.1.2.2 Parser class API</a>
</div>
<hr /></div>
</div>
<!--End of Navigation Panel-->

<h3><a name="SECTION009121000000000000000">
7.1.2.1 FeedParser API</a>
</h3>

<p>

<span class="versionnote">New in version 2.4.</span>

<p>
The <tt class="class">FeedParser</tt>, imported from the <tt class="module">email.feedparser</tt> module,
provides an API that is conducive to incremental parsing of email messages,
such as would be necessary when reading the text of an email message from a
source that can block (e.g. a socket).  The
<tt class="class">FeedParser</tt> can of course be used to parse an email message fully
contained in a string or a file, but the classic <tt class="class">Parser</tt> API may be
more convenient for such use cases.  The semantics and results of the two
parser APIs are identical.

<p>
The <tt class="class">FeedParser</tt>'s API is simple; you create an instance, feed it a
bunch of text until there's no more to feed it, then close the parser to
retrieve the root message object.  The <tt class="class">FeedParser</tt> is extremely
accurate when parsing standards-compliant messages, and it does a very good
job of parsing non-compliant messages, providing information about how a
message was deemed broken.  It will populate a message object's <var>defects</var>
attribute with a list of any problems it found in a message.  See the
<tt class="module"><a href="module-email.errors.html">email.errors</a></tt> module for the list of defects that it can find.

<p>
Here is the API for the <tt class="class">FeedParser</tt>:

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><span class="typelabel">class</span>&nbsp;<tt id='l2h-1240' xml:id='l2h-1240' class="class">FeedParser</tt></b>(</nobr></td>
  <td><var></var><big>[</big><var>_factory</var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
Create a <tt class="class">FeedParser</tt> instance.  Optional <var>_factory</var> is a
no-argument callable that will be called whenever a new message object is
needed.  It defaults to the <tt class="class">email.message.Message</tt> class.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-1241' xml:id='l2h-1241' class="method">feed</tt></b>(</nobr></td>
  <td><var>data</var>)</td></tr></table></dt>
<dd>
Feed the <tt class="class">FeedParser</tt> some more data.  <var>data</var> should be a
string containing one or more lines.  The lines can be partial and the
<tt class="class">FeedParser</tt> will stitch such partial lines together properly.  The
lines in the string can have any of the common three line endings, carriage
return, newline, or carriage return and newline (they can even be mixed).
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-1242' xml:id='l2h-1242' class="method">close</tt></b>(</nobr></td>
  <td><var></var>)</td></tr></table></dt>
<dd>
Closing a <tt class="class">FeedParser</tt> completes the parsing of all previously fed data,
and returns the root message object.  It is undefined what happens if you feed
more data to a closed <tt class="class">FeedParser</tt>.
</dl>

<p>

<div class="navigation">
<div class='online-navigation'>
<p></p><hr />
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="7.1.2 parsing email messages"
  href="module-email.parser.html"><img src='../icons/previous.png'
  border='0' height='32'  alt='Previous Page' width='32' /></a></td>
<td class='online-navigation'><a rel="parent" title="7.1.2 parsing email messages"
  href="module-email.parser.html"><img src='../icons/up.png'
  border='0' height='32'  alt='Up one Level' width='32' /></a></td>
<td class='online-navigation'><a rel="next" title="7.1.2.2 parser class API"
  href="node149.html"><img src='../icons/next.png'
  border='0' height='32'  alt='Next Page' width='32' /></a></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
  href="contents.html"><img src='../icons/contents.png'
  border='0' height='32'  alt='Contents' width='32' /></a></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
  border='0' height='32'  alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
  href="genindex.html"><img src='../icons/index.png'
  border='0' height='32'  alt='Index' width='32' /></a></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="module-email.parser.html">7.1.2 Parsing email messages</a>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="module-email.parser.html">7.1.2 Parsing email messages</a>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="node149.html">7.1.2.2 Parser class API</a>
</div>
</div>
<hr />
<span class="release-info">Release 2.5.1, documentation updated on 18th April, 2007.</span>
</div>
<!--End of Navigation Panel-->
<address>
See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
</address>
</body>
</html>
